<template>
  <div>
    <h1>修改理赔信息</h1>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="订单编号" prop="ordersId">
        <el-input v-model="ruleForm.ordersId"></el-input>
      </el-form-item>
      <el-form-item label="理赔原因" prop="reason">
        <el-input type="textarea" v-model="ruleForm.reason"></el-input>
      </el-form-item>
      <div class="claim-box">
        <h5 style="margin-bottom: 10px">修改理赔凭证</h5>
        <el-upload
          class="avatar-uploader"
          action="http://localhost:3000/images/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="ruleForm.img" :src="'http://localhost:3000/temp/' + ruleForm.img" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <el-form-item>
        <el-button type="primary" @click="updateOneClaim">立即修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from "../../../network/apis/api";
export default {
  created() {
    this.getOneClaim()
  },
  data() {
    return {
      image: "",
      ruleForm: {
        ordersId: this.$route.params._id,
        startTime: "",
        salesmanId: localStorage._id,
        reason: "",
        img: "",
        state: 0,
        _id:""
      },
      rules: {
        ordersId: [
          { required: true, message: "请输入订单编号", trigger: "blur" },
          {
            min: 18,
            max: 28,
            message: "长度在 18 到 28 个字符",
            trigger: "blur",
          },
        ],
        reason: [
          { required: true, message: "请填写理赔原因", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
      
      async getOneClaim(){
        const data = await api.claims.getOneClaim(this.$route.params._id);
        this.ruleForm = data.data.data;
        console.log(this.ruleForm);
      },

      async updateOneClaim() {
      const data = await api.claims.updateOneClaim(this.ruleForm);
      console.log(data);
      if (data.status) {
        this.$router.push("/salesman/getAllClaims");
      }
    },

    // async getOneClaim() {
    //   const data = await api.claims.getOneClaim(this.ruleForm);
    //   if (data.status) {
    //     this.ruleForm = {};
    //     this.image = "";
    //   }
    // },
    resetForm(formName) {
      console.log(this.ruleForm);
      // this.$refs[formName].resetFields();
    },
    //上传图片
    handleAvatarSuccess(res, file) {
      console.log(res.data);
      this.iamge = res.data;
      this.ruleForm.img = res.data;
    },
    //限制图片上传的大小
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>